﻿

#include "CustomEditBox.h"

using namespace std;;

CUSTOM_UI_BEGIN

CustomEditBox::CustomEditBox(void)
:_editBox(nullptr)
, _checkCNInput(false)
, _changeFlag(false)
, _editingendcallback(nullptr)
{

}
CustomEditBox::~CustomEditBox()
{

}

CustomEditBox* CustomEditBox::create(const Size& size, Scale9Sprite* normalSprite)
{
	CustomEditBox* pRe = new CustomEditBox();
	if (pRe && pRe->initWithSizeAndBackgroundSprite(size,normalSprite))
	{
		pRe->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRe);
	}
	return pRe;
}

CustomEditBox* CustomEditBox::create(const Size& size,
	const std::string& normal9SpriteBg,
	TextureResType texType /* = TextureResType::LOCAL */)
{
	CustomEditBox* pRe = new CustomEditBox();
	if (pRe && pRe->initWithSizeAndBackgroundSprite(size, normal9SpriteBg,texType))
	{
		pRe->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRe);
	}
	return pRe;
}

bool CustomEditBox::initWithSizeAndBackgroundSprite(const Size& size, Scale9Sprite* normal9SpriteBg)
{
	_editBox = EditBox::create(size, normal9SpriteBg);
	if (_editBox && Node::init())
	{
		_editBox->setDelegate(this);
		addChild(_editBox);
		return true;
	}
	return false;
}

bool CustomEditBox::initWithSizeAndBackgroundSprite(const Size& size,
	const std::string& normal9SpriteBg,
	TextureResType texType /* = TextureResType::LOCAL */)
{
	_editBox = EditBox::create(size, normal9SpriteBg, texType);
	if (_editBox && Node::init())
	{
		_editBox->setDelegate(this);
		addChild(_editBox);
		return true;
	}
	return false;
}


void CustomEditBox::setEnabled(bool enabled)
{
	if (_editBox)
	{
		_editBox->setEnabled(enabled);
	}
}

void CustomEditBox::setText(const char* pText)
{
	if (_editBox)
	{
		_editBox->setText(pText);
	}
}
            
const char* CustomEditBox::getText()
{
	if (_editBox)
	{
		return _editBox->getText();
	}
	return nullptr;
}

void CustomEditBox::setFont(const char* pFontName, int fontSize)
{
	if (_editBox)
	{
		_editBox->setFont(pFontName, fontSize);
	}
}
            
void CustomEditBox::setFontName(const char* pFontName)
{
	if (_editBox)
	{
		_editBox->setFontName(pFontName);
	}
}
            
 
void CustomEditBox::setFontSize(int fontSize)
{
	if (_editBox)
	{
		_editBox->setFontSize(fontSize);
	}
}

void CustomEditBox::setFontColor(const Color3B& color)
{
	if (_editBox)
	{
		_editBox->setFontColor(color);
	}
}

void CustomEditBox::setFontColor(const Color4B& color)
{
	if (_editBox)
	{
		_editBox->setFontColor(color);
	}
}

void CustomEditBox::setPlaceholderFont(const char* pFontName, int fontSize)
{
	if (_editBox)
	{
		_editBox->setPlaceholderFont(pFontName,fontSize);
	}
}
            
void CustomEditBox::setPlaceholderFontName(const char* pFontName)
{
	if (_editBox)
	{
		_editBox->setPlaceholderFontName(pFontName);
	}
}
            
void CustomEditBox::setPlaceholderFontSize(int fontSize)
{
	if (_editBox)
	{
		_editBox->setPlaceholderFontSize(fontSize);
	}
}
            
void CustomEditBox::setPlaceholderFontColor(const Color3B& color)
{
	if (_editBox)
	{
		_editBox->setPlaceholderFontColor(color);
	}
}

void CustomEditBox::setPlaceholderFontColor(const Color4B& color)
{
	if (_editBox)
	{
		_editBox->setPlaceholderFontColor(color);
	}
}
            
void CustomEditBox::setPlaceHolder(const char* pText)
{
	if (_editBox)
	{
		_editBox->setPlaceHolder(pText);
	}
}
            
const char* CustomEditBox::getPlaceHolder()
{
	if (_editBox)
	{
		return _editBox->getPlaceHolder();
	}
	return nullptr;
}
            
void CustomEditBox::setInputMode(EditBox::InputMode inputMode)
{
	if (_editBox)
	{
		_editBox->setInputMode(inputMode);
	}
}

void CustomEditBox::setMaxLength(int maxLength)
{
	if (_editBox)
	{
		_editBox->setMaxLength(maxLength);
	}
}
            
int CustomEditBox::getMaxLength()
{
	if (_editBox)
	{
		return _editBox->getMaxLength();
	}
	return 0;
}
            
void CustomEditBox::setInputFlag(EditBox::InputFlag inputFlag)
{
	if (_editBox)
	{
		return _editBox->setInputFlag(inputFlag);
	}
}
void CustomEditBox::setCheckCNInput(bool isCheck)
{
	_checkCNInput = isCheck;
}

bool CustomEditBox::getCheckCNInput()
{
	return _checkCNInput;
}

void CustomEditBox::addEditingEndCallback(std::function<void()> func)
{
	this->_editingendcallback = func;
}



void CustomEditBox::editBoxEditingDidBegin(EditBox* editbox)
{

}

void CustomEditBox::editBoxEditingDidEnd(EditBox* editBox)
{
	if (_editingendcallback)
	{
		_editingendcallback();
	}
}

void CustomEditBox::editBoxTextChanged(EditBox* editBox, const std::string& text)
{
	if (_checkCNInput)
	{
		int len = text.length();
		if (len <= 0)
			return;

		string str = text;
		int num = CheckStrCN(str);
		if (num != len + 1)       //有中文
		{
			_editBox->setText("");
			_editBox->setPlaceHolder("");
			_changeFlag = true;
		}
	}
}

void CustomEditBox::editBoxReturn(EditBox* editBox)
{

}

int CustomEditBox::CheckStrCN(std::string &str)
{
	int iStrLen = str.length();
	unsigned char tmp_1 = 0;

	if (0 == iStrLen)
	{
		return 0;  // 无效编码
	}

	for (int i = 0; i < (int)str.length(); ++i)
	{
		tmp_1 = str[i];
		if (tmp_1 < 32 || tmp_1 > 127)   // 标准的字符
			return i;
	}

	return str.length() + 1;
}

CUSTOM_UI_END
